package com.example.testproject.activity

import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.databinding.DataBindingUtil
import coil.ImageLoader
import coil.ImageLoaderFactory
import coil.load
import coil.request.ImageRequest
import coil.util.CoilUtils
import com.example.testproject.R
import com.example.testproject.databinding.CoilDataBinDing
import com.example.testproject.ext.click
import com.example.testproject.utils.toast
import okhttp3.OkHttpClient
import java.security.AccessController.getContext


/**
 *
 * @ClassName: CoilActivity
 * @Author: android 超级兵
 * @CreateDate: 1/26/22$ 7:34 PM$
 * TODO coil页面
 * 官方地址:https://github.com/coil-kt/coil/
 *
 * 中文文档: https://github.com/coil-kt/coil/blob/main/README-zh.md
 *
 * 完整文档(英文): https://coil-kt.github.io/coil/getting_started/
 */
class CoilActivity : AppCompatActivity(), ImageLoaderFactory {

    private val dataBinDing: CoilDataBinDing by lazy {
        DataBindingUtil.setContentView(this, R.layout.activity_coil)
    }

    private val androidUrl =
        "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhanjietech.cn%2Fuploads%2Fmanage%2Fimages%2F3%2F20200123%2Fb6907f67b8156c7c48c06d6f0ada59c4.jpg&refer=http%3A%2F%2Fhanjietech.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645789513&t=d3ffa46b8260be5e8096c903fd32275d"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        // 网络图片加载
        dataBinDing.bt1.click {
            dataBinDing.image.load(androidUrl) {
                // 淡入淡出
                crossfade(true)
                // 加载中占位符显示
                placeholder(R.mipmap.no_data)
                // 图片圆角
//                transformations(CircleCropTransformation())

                // 高斯模糊 [使用设备 4.3以上]
//                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
//                    transformations(
//                        BlurTransformation(
//                            this@CoilActivity,
//                            radius = 25f,// 【高斯模糊 取值范围0-25】
//                            sampling = 0.2f // 采样倍率 [范围:0-1]
//                        )
//                    )
//                }

                // 灰度
//                transformations(GrayscaleTransformation())

                // 圆角角度
//                transformations(RoundedCornersTransformation(topLeft = 25f))
            }
        }

        // 本地图片加载
        dataBinDing.bt2.click {
            dataBinDing.image.load(R.mipmap.guidao_a)
        }

        // ImageLoader
        dataBinDing.bt3.click {
            val imageLoader = ImageLoader.Builder(this)
                // 占用内存百分比
                .availableMemoryPercentage(0.25)
                .crossfade(true)
                .build()

            dataBinDing.image.load(androidUrl, imageLoader = imageLoader)
        }

        //
        dataBinDing.bt4.click {
            val imageLoader = ImageLoader.Builder(this)
                // 占用内存百分比
                .availableMemoryPercentage(0.25)
                .crossfade(true)
                .build()

            ImageRequest.Builder(this)
                .data(androidUrl) // 设置路径
                .target(
                    dataBinDing.image,// 设置ImageView
                    /* onStart = {
                     // Handle the placeholder drawable.
                 },
                     onSuccess = {
                         // Handle the successful result.
                     },
                     onError = {
                         // Handle the error drawable.
                     }*/
                )
                .build().apply {
                    // 同步执行
                    imageLoader.enqueue(this)

                    // 异步执行
//                    lifecycleScope.launch {
//                        imageLoader.execute(this@apply)
//                    }
                }
        }

        dataBinDing.bt5.click {
            dataBinDing.image.load(androidUrl, imageLoader = newImageLoader())

        }

        // 设置全局ImageLoader
//        Coil.setImageLoader(newImageLoader())


        dataBinDing.search.isSubmitButtonEnabled = true

        dataBinDing.search.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String): Boolean {
                "确定:${query}" toast this@CoilActivity
                // true 软键盘不关闭
                return false
            }

            override fun onQueryTextChange(newText: String): Boolean {
                this@CoilActivity toast "输入中:${newText}"
                // true 软键盘不关闭
                return false
            }
        })
    }

    /*
     * 作者:android 超级兵
     * 创建时间: 1/26/22 8:16 PM
     * TODO 需要实现 ImageLoaderFactory 方法, 正确打开方式应该写到Application中共全局使用
     */
    override fun newImageLoader() = let {
        ImageLoader.Builder(this)
            // 渐入渐出动画
            .crossfade(true)
            .okHttpClient {
                OkHttpClient.Builder()
                    .cache(CoilUtils.createDefaultCache(this))
                    .build()
            }.build()
    }
}